package eyrolles;

import exceptions.EmployeNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionMessage;

public class GetEmployeAction extends ActionDB {

    protected ActionForm buildEmployeForm(String username) throws SQLException,
            EmployeNotFoundException {

        String user = null;
        EmployeForm form = null;

        try {

            conn = getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery("select * from employes where username=\'"
                    + username + "'");

            if (rs.next()) {

                form = new EmployeForm();

                form.setUsername(rs.getString("username"));
                form.setPassword(rs.getString("password"));
                form.setDepid(rs.getString("depid"));
                form.setRoleid(rs.getString("roleid"));
                String name = rs.getString("name");
                System.err.println("---->" + name + "<----");
                form.setName(name);
                form.setPhone(rs.getString("phone"));
                form.setEmail(rs.getString("email"));
            } else {

                throw new EmployeNotFoundException("Employe " + username + " non trouve!");
            }
        } catch (NullPointerException e) {

            throw new SQLException();
        } finally {

            closeConnection();
        }
        return form;
    }

    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException {

        // Cible par defaut en cas de succes
        String target = new String("success");

        ActionMessages errors = new ActionMessages();

        // Teste sur l'identification de l'utilisateur
        HttpSession session = request.getSession();
        if (session.getAttribute("USER") == null) {

            // L'utilisateur n'est pas identifie
            target = new String("login");

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.login.required"));

            // Retourner les erreurs eventuelles au formulaire d'origine
            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }

            // Si jamais l'utilisateur n'est pas loggé, alors
            // on retourne directement à la page de login
            // avant d'effectuer la moindre requête
            return (mapping.findForward(target));
        }

        if (isCancelled(request)) {

            // Annulation depuis la liste des employes
            return (mapping.findForward(target));
        }

        try {

            form = buildEmployeForm(request.getParameter("username"));

            if (form != null) {

                if ("request".equals(mapping.getScope())) {

                    request.setAttribute(mapping.getAttribute(), form);
                } else {
                    session = request.getSession();
                    session.setAttribute(mapping.getAttribute(), form);
                }
            } else {

                errors.add(ActionMessages.GLOBAL_MESSAGE,
                        new ActionMessage("errors.form.error"));
            }

        } catch (EmployeNotFoundException e) {


            target = new String("error");
            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.database.error", e.getMessage()));

        } catch (SQLException s) {

            target = new String("error");

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.database.error"));
        }


        // Retourner les erreurs eventuelles
        if (!errors.isEmpty()) {

            saveErrors(request, errors);
        }

        // Transmettre la requete a la vue appropriee
        return (mapping.findForward(target));
    }
}